#include("../../_common/_header.html")

<div class="layui-btn-container">
	<a onclick="window.location.href='#(ctx)/admin/goods/edit?id=#(goods.id)'" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-return"></i>编辑基本信息</a>
	#if(null != goods.id)
		<a onclick="window.location.href='#(ctx)/admin/goods'" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-list"></i>返回列表</a>
	#end
</div>
<hr class="layui-bg-blue">
<blockquote class="layui-elem-quote">商品布局编辑</blockquote>

<table class="layui-table" lay-skin="nob">
	<colgroup>
		<col width="80px">
		<col width="400px">
		<col>
	</colgroup>
	<tbody>
		<tr>
			<td align="right">商品标题：</td>
			<td>#(goods.title)</td>
		</tr>
		<tr>
			<td align="right">商品标签：</td>
			<td>#(goods.label)</td>
		</tr>
	</tbody>
</table>

<fieldset class="layui-elem-field">
	<legend>布局概览</legend>
	<div class="layui-field-box">
		<div class="layui-form-item">
			<div class="layui-inline">
				<ul class="layui-timeline">
				#for(place : goods.goodsImagePlaceList)
					<li class="layui-timeline-item">
						<i class="layui-icon layui-timeline-axis"></i>
						<div class="layui-timeline-content layui-text">
							<h3 class="layui-timeline-title">#(place.name)</h3>
							<p>
								<div class="layui-card-body">
								#if(place.code == "goods_content")
									<< 此处将显示基本信息中录入的商品详情内容 >>
								#else
									#for(image : goods.goodsImageList)
									#if(image.placeCode == place.code && image.placeId == place.id)
									<img onclick="imgReview(this);" src="#(image.thumbImg)" style="max-height: 48px; max-width: 48px">
									#end
									#end
								#end
								</div>
							</p>
						</div>
					</li>
				#end
				</ul>
			</div>
		</div>
	</div>
</fieldset>

<form class="layui-form" action="#(ctx)/admin/goods/updateLayout" method="post">
#include("_layoutForm.html")
</form>

<hr class="layui-bg-blue">
<div class="layui-btn-container">
	<a onclick="window.location.href='#(ctx)/admin/goods/edit?id=#(goods.id)'" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-return"></i>编辑基本信息</a>
	#if(null != goods.id)
		<a onclick="window.location.href='#(ctx)/admin/goods'" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-list"></i>返回列表</a>
	#end
</div>

<script src="#(ctx)/frame/layui/layui.all.js" charset="utf-8"></script>

<script type="text/javascript">
layui.use('upload', function(){
	var $ = layui.$, upload = layui.upload;

	//多图片上传
	upload.render({
		elem : '#ID_upload_img_btn',
		url : '#(ctx)/admin/upload/ajaxUpload',
		accept : 'images', // 可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
		multiple : true,
		size : 800, // 限制文件大小，单位 KB
		done : function(res, index, upload) {
			if(res.code==0){
				addGoodsImage(res);
			}

		},error: function(index, upload){
			layer.alert("上传失败");
		},allDone(obj){ //obj.total 得到总文件数; obj.successful 请求成功的文件数; obj.aborted 请求失败的文件数
			window.location.href="#(ctx)/admin/goods/editLayout?id=" + #(goods.id);
		}
	});
});

// 追加图片到商品
function addGoodsImage(res){
	var originalImg, thumbImg;
	for(var i = 0; i < res.count; i++){
		originalImg = eval("res.imgUrl" + i); // 取得图片对应的Key
		thumbImg = originalImg;
	}

	layui.$.ajax({ // 用Ajax替换弹窗中的提交
        url     : "#(ctx)/admin/goods/ajaxAddGoodsImage",
        type    : "post",
        data    : {goodsId : #(goods.id), originalImg, thumbImg}, //获取提交的数据
        success : function(ret){
        	if(ret.code == 0){
        	} else {
        		if(ret.msg){
        			layer.alert(ret.msg); // Ajax调用成功，但后台返回失败信息
        		} else {
        			layer.alert("商品图片更新失败！");
        		}
        	}
        },
        error : function (e) { // Ajax调用失败
        	layer.alert("商品图片更新失败：" + e.responseText);
        }
	});
}
layui.form.render('select');

// 删除商品图片
function delGoodsImage(id){
	var index = layer.confirm('确认删除此记录么？', function(index){
		layui.$.ajax({ // 用Ajax替换弹窗中的提交
	        url     : "#(ctx)/admin/goods/ajaxDelGoodsImage?id=" + id,
	        type    : "post",
	        success : function(ret){
	        	if(ret.code == 0){
	        		layui.$("#ID_goods_image_dev_" + id).remove();
	        	} else {
	        		if(ret.msg){
	        			layer.alert(ret.msg); // Ajax调用成功，但后台返回失败信息
	        		} else {
	        			layer.alert("商品图片删除失败！");
	        		}
	        	}
	        },
	        error : function (e) { // Ajax调用失败
	        	layer.alert("商品图片删除失败：" + e.responseText);
	        }
		});
		layer.close(index);
	});
}

//预览图片
function imgReview(obj){
	layer.photos({
		photos: layui.$(obj).parent()
	});
}
</script>

#include("../../_common/_footer.html")
